$(function (){







	//通过品牌ID值的获取,得到所有的品种,并显示到HTML
	$("#type_add_brand").on("change",function (){
		//select * from tb_variety where brand_id = #{brandId}
		$.post("/variety/forBrandId",{"brandId":$(this).val()},function (data){
			//通过jquery实现数据的动态添加.
			$("#type_add_variety").empty();
			if(data.length==0){
				$("#type_add_variety").append("<option value='0'>请添加品种后再添加类型</option>");
			}else{
				$("#type_add_variety").append("<option value='0'>请选择品种</option>");
			}
			for (var i in data) {
				var variety = data[i];
				$("#type_add_variety").append("<option value='"+variety.id+"'>"+variety.name+"</option>");
			}
		})
	})




	$("#type").on("click",function (){
		$("#type_div").removeClass("layui-hide");
		$("#brand_div").addClass("layui-hide");
		$("#variety_div").addClass("layui-hide");
	})

	$("#brand").on("click",function (){
		$("#brand_div").removeClass("layui-hide");
		$("#type_div").addClass("layui-hide");
		$("#variety_div").addClass("layui-hide");
	})

	$("#variety").on("click",function (){
		$("#variety_div").removeClass("layui-hide");
		$("#brand_div").addClass("layui-hide");
		$("#type_div").addClass("layui-hide");

		$("#variety_choose").empty();
		$("#variety_choose").append("<option value='0'>请根据品牌查看所有的品种</option>");

		$("#brand_list2").empty();
		$("#brand_list2_content").empty();

		$.post("/brand/all",function (data){
			//	添加到select选择框内
			for (var i in data) {
				var brand = data[i];
				$("#variety_choose").append("<option value='"+brand.id+"'>"+brand.name+"</option>");
				$("#brand_list2").append("<li >"+brand.name+"</li>");
				$("#brand_list2_content").append("<div class='layui-tab-item' >\n" +
					"\t\t\t\t\t\t\t\t\t<form action='/variety/add' class='layui-container' style='margin-top: 30px;' >\n" +
					"\t\t\t\t\t\t\t\t\t\t<input type='hidden' name='brandId' value='"+brand.id+"'>\n" +
					"\t\t\t\t\t\t\t\t\t\t<input class='variety_add_ipt layui-input-inline' type='text' name='name' style='width: 500px;'  placeholder='请添加新的品种' >\n" +
					"\t\t\t\t\t\t\t\t\t\t<input class='variety_add_sub layui-input-inline layui-btn-normal' type='submit' value='添加'>\n" +
					"\t\t\t\t\t\t\t\t\t</form>\n" +
					"\t\t\t\t\t\t\t\t</div>");
			}

		})
	})

	$("#brand_add_sub").on("click",function (){
		$.ajax({
			url:"/brand/add",
			method:"post",
			data:{name:$("#brand_add_ipt").val()},
			success:function (data){
				console.log("添加" + data);
				location.href="/brand/list";
			}
		})
		return false;
	})

})